package frame.exec 
{
	import common.Struct;
	import common.StructStaff;
	import data.DataMonitor;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	import frame.IFrame;
	import nsdecl.ns_group_handler;
	import nsdecl.ns_group_hook;
	import nsdecl.ns_interactive_slot;
	import ui.basePannel;
	import ui.ui_button;
	import ui.ui_check_box;
	import ui.ui_icon_button;
	import ui.ui_scroll_bar;
	import ui.ui_text;
	import ui.uiGroup;
	import ui.uiRadioGroup;
	import ui.uiUnit;
	import ui.uiWin;
	import ui.utils.dialogProperty;
	import ui.utils.reqDialogService;
	import utils.range;
	import utils.safeCallback;
	import vndecl.*;
	
	/**
	 * ...
	 * @author anod
	 */
	public class SetStaffSchedule extends basePannel implements IFrame 
	{
		public static const WORKDAY_OPTION_MAX:int = 5;		//类型数量
		public static const WDOPT_NORMAL:int = 0;			//当天按照正常每周工作日排班
		public static const WDOPT_SKIP:int = 1;				//当天有事，请求换一天排班
		public static const WDOPT_CHOSEN:int = 2;			//当天必须到
		public static const WDOPT_VOCATION:int = 3;			//当天不出勤，总工作日-1
		public static const WDOPT_EXTRA:int    = 4;			//当天安排加班，总工作日+1
		public static const WDOPT_CUSTOM:int   = 5;			//自定义排班
		
		private static const WDCUSTOM_CONFIG:String 	= "config";
		private static const WDCUSTOM_FROM:String   	= "from";
		private static const WDCUSTOM_DAYCNT:String 	= "daycnt";
		private static const WDCUSTOM_WDCNT:String  	= "wdcnt";
		
		private var mFrame:FSetExecStaff;
		
		[Embed(source="../../../lib/include_day.png")]
		private var mDayCheck:Class;
		private var mDayChkbmp:BitmapData;
		
		/**
		 * 对于每一个员工的工作日期，保存当天的配置内容
		 * 0 按照正常的5/7排班
		 * 1 - 4：如上面枚举
		 * 其它：自定义，用来查表mWdCustomTable
		 */
		private var mWorkdayConfig:Dictionary;
		
		/**
		 * 对于自定义类型的日期，用来表示具体配置
		 * key - 某个大于5的数值
		 * value - { config:a, from:b, daycnt:c, wdcnt:d }其中a==key，b是开始日期的毫秒，c是连续天数，d是工作天数
		 */
		private var mWdCustomTable:Dictionary;
		
		/**
		 * 这个变量记录每个班次中哪些日期当前被选中了。
		 * key - 班次ID value - 单个元素uiUnit
		 */
		private var mSelectedEditday:Dictionary;
		
		public function SetStaffSchedule( frame:FSetExecStaff ) 
		{
			mFrame = frame;
			addChild( mFrame );
			super();
			
			mDayChkbmp = new mDayCheck().bitmapData;
			
			setupInteractive();
			setupDataMonitor();
		}
		
		/* INTERFACE frame.IFrame */
		
		public function setupInteractive():void 
		{
			iNextButton.click   = "nextPanel";
			iPrevButton.click   = "prevPanel";
			iCancelButton.click = "switchChild";
			iStaffTable.handler = "listStaffOption";
			iStaffTable.hook    = "scrollable";
			iScrollBar.area     = "iStaffTable";
			iScrollBar.scroll   = "scrollPlaceSprite";
		}
		
		public function setupDataMonitor():void 
		{
			DataMonitor.updateData( range(WORKDAY_OPTION_MAX, 1), VNPC.SETSTAFFSCHEDULE_OPTIONS );
			
			iStaffTable.data    = VNGM.PROXY_STAFF;
		}
		
		// UI回调
		//------------------------------------
		ns_group_handler function listStaffOption( entry:uiUnit, value:StructStaff ):void
		{
			var content:UStaffOption = new UStaffOption();
			
			content.iNameText.component.text = "员工：" + value.name;
			
			content.iOptions.component.data     = VNPC.SETSTAFFSCHEDULE_OPTIONS;
			content.iOptions.component.handler  = "listWorkdayOptions";
			content.iOptions.component.onselect = "onCheckOption";
			content.iOptions.component.auto     = false;
			content.iOptions.visible 			= false;
			
			content.iMonthTable.component.data = VNIC.EXEC_MONTHDATA;
			content.iMonthTable.component.handler = "listWeek";
			
			entry.addChild( content );
		}
		ns_group_handler function listWeekday( entry:uiUnit, day_info:int ):void
		{
			reqDialogService( this, "listWeekday", [entry, day_info] );
			
			var bmp:Bitmap = new Bitmap( );
			entry.addChild( bmp );
			
			entry.click = "onDayOption";
		}
		ns_interactive_slot function onDayOption( entry:uiUnit ):void
		{
			var staff:StructStaff = getStaffFrom(entry);
			
			// 取消原先已经选中的，选择新选中的
			setDaysUncheck( getAllMonths( entry ) );
			var old_entry:uiUnit = mSelectedEditday[ staff.ID ];
			setDayChecked( entry, old_entry != entry );
			mSelectedEditday[ staff.ID ] = old_entry == entry ? null : entry;
			
			// 开关选项
			var ps:PlaceSprite = dialogProperty( entry.parent, "iOptions", typeof(this) );
			ps.visible = mSelectedEditday[ staff.ID ] != null;
		}
		ns_group_handler function listWorkdayOptions( entry:uiUnit, value:int ):void
		{
			var button:ui_check_box = new ui_check_box();
			
			const desc:Array = ["调班","补班","休假","加班","自定义"];
			button.text = desc[ entry.key ];
			button.show_display();
			
			entry.addChild( button );
			if ( value == WDOPT_CUSTOM )
			{
				var control:UStaffCustom = new UStaffCustom();
				initStaffCustom( control );
				entry.addChild( control );
				control.visible = false;
			}
			
			entry.select = "onSelectOption";
		}
		ns_interactive_slot function onSelectOption( entry:uiUnit, selected:Boolean ):void
		{
			var button:ui_check_box = entry.getChildAt(0) as ui_check_box;
			button.check = selected;
			if ( entry.data == WDOPT_CUSTOM ) 
			{
				entry.getChildAt( entry.numChildren - 1 ).visible = selected;
			}
		}
		ns_interactive_slot function onCheckOption( index:int, entry:uiUnit ):void
		{
			var button:ui_check_box = entry.getChildAt(0) as ui_check_box;
		}
		ns_group_hook function scrollable( group:uiGroup ):void
		{
			mSelectedEditday = new Dictionary();
			iScrollBar.visible = iStaffTable.height > mFrame.iStaffTable.scrollRect.height;
		}
		
		ns_interactive_slot function showButtonForAdjust( txt:ui_text ):void
		{
			setButtonForAdjust( txt, true );
		}
		ns_interactive_slot function closeButtonForAdjust( txt:ui_text ):void
		{
			setButtonForAdjust( txt, false );
		}
		ns_interactive_slot function showButtonOnHover( button:ui_icon_button ):void
		{
			button.parent.alpha = 1.0;
		}
		ns_interactive_slot function closeButtonOnLeave( button:ui_icon_button ):void
		{
			button.parent.alpha = 0.0;
		}
		ns_interactive_slot function adjustDayCount( button:ui_icon_button ):void
		{
			var ps:PlaceSprite = button.parent as PlaceSprite;
			var token:String = ps.name.slice(0, -3);
			var num:PlaceSprite = dialogProperty( ps, token + "Num", typeof(this) );
			var cnt:int = num.component.text;
			cnt += ( ps.name.slice( -3) == "Inc" ) ? 1 : -1;
			if ( cnt <= 1 ) cnt = 1;
			num.component.text = cnt;
			num = dialogProperty( ps, "iWorkdayNum", typeof( this ) );
			if ( num.component.text > cnt ) num.component.text = cnt;
		}
		ns_interactive_slot function adjustWorkdayCount( button:ui_icon_button ):void
		{
			var ps:PlaceSprite = button.parent as PlaceSprite;
			var token:String = ps.name.slice(0, -3);
			var num:PlaceSprite = dialogProperty( ps, token + "Num", typeof(this) );
			var txt_num:ui_text = num.component;
			var cnt:int = num.component.text;
			cnt += ( ps.name.slice( -3) == "Inc" ) ? 1 : -1;
			if ( cnt <= 0 ) cnt = 0;
			num = dialogProperty( ps, "iDaysNum", typeof( this ) );
			if ( cnt > num.component.text ) cnt = num.component.text;
			txt_num.text = String( cnt );
		}
		
		//常用函数
		//------------------------------------
		private function initStaffCustom( com:UStaffCustom ):void
		{
			com.iDaysDec.alpha = com.iDaysInc.alpha = 0;
			com.iWorkdayDec.alpha = com.iWorkdayInc.alpha = 0;
			
			com.iWorkdayNum.component.over = com.iDaysNum.component.over = "showButtonForAdjust";
			com.iWorkdayNum.component.out = com.iDaysNum.component.out = "closeButtonForAdjust";
			
			com.iDaysDec.component.over = com.iWorkdayDec.component.over = "showButtonOnHover";
			com.iDaysInc.component.over = com.iWorkdayInc.component.over = "showButtonOnHover";
			com.iDaysDec.component.out = com.iWorkdayDec.component.out = "closeButtonOnLeave";
			com.iDaysInc.component.out = com.iWorkdayInc.component.out = "closeButtonOnLeave";
			
			com.iDaysDec.component.click = com.iDaysInc.component.click = "adjustDayCount";
			com.iWorkdayDec.component.click = com.iWorkdayInc.component.click = "adjustWorkdayCount";
		}
		private function setButtonForAdjust( txt:ui_text, show:Boolean ):void
		{
			var ps:PlaceSprite = txt.parent as PlaceSprite;
			var token:String = ps.name.slice(0, -3);
			var inc:PlaceSprite = dialogProperty( txt, token + "Inc", typeof(this) );
			var dec:PlaceSprite = dialogProperty( txt, token + "Dec", typeof(this) );
			inc.alpha = dec.alpha = show ? 1 : 0;
		}
		private function setDaysUncheck( group:uiGroup ):void
		{
			for ( var i:int = 0; i < group.numChildren; ++i )
			{
				var month_unit:uiUnit = group.getChildAt(i) as uiUnit;
				var daily_group:uiGroup = month_unit.getChildAt( month_unit.numChildren - 1 ) as uiGroup;
				for ( var j:int = 0; j < daily_group.numChildren; ++j )
				{
					var day:uiUnit = daily_group.getChildAt(j) as uiUnit;
					var chkbmp:Bitmap = day.getChildAt( day.numChildren - 1 ) as Bitmap;
					chkbmp.bitmapData = null;
				}
			}
		}
		private function setDayChecked( entry:uiUnit, check:Boolean ):void
		{
			var chkbmp:Bitmap = entry.getChildAt( entry.numChildren - 1 ) as Bitmap;
			chkbmp.bitmapData = check ? mDayChkbmp : null;
		}
		private function getStaffFrom( entry:uiUnit ):StructStaff
		{
			var all_month_group:uiGroup = getAllMonths( entry );
			var ps:PlaceSprite = all_month_group.parent as PlaceSprite;
			var content:UStaffOption = ps.parent as UStaffOption;
			return uiUnit( content.parent ).data;
		}
		private function getAllMonths( entry:uiUnit ):uiGroup
		{
			var month_group:uiGroup = entry.owner;
			return month_group.parent.parent as uiGroup;
		}
		
		// 得到数据内容
		//------------------------------------
		private function getConfig( id:int ):int
		{
			var container:Object = mWdCustomTable[id];
			return container[ WDCUSTOM_CONFIG ];
		}
		private function getTimeBegin( id:int ):Number
		{
			var container:Object = mWdCustomTable[id];
			return container[ WDCUSTOM_FROM ];
		}
		private function getDayBegin( id:int ):Date
		{
			var retval:Date = new Date();
			retval.setTime( getTimeBegin(id) );
			return retval;
		}
		private function getDaysLength( id:int ):int
		{
			var container:Object = mWdCustomTable[id];
			return container[ WDCUSTOM_DAYCNT ];
		}
		private function getWorkdaysLength( id:int ):int
		{
			var container:Object = mWdCustomTable[id];
			return container[ WDCUSTOM_WDCNT ];
		}
		
		//交互组件定义
		//------------------------------------
		public function get iNextButton():ui_button
		{
			return mFrame.iNextButton.component;
		}
		public function get iPrevButton():ui_button
		{
			return mFrame.iPrevButton.component;
		}
		public function get iCancelButton():ui_button
		{
			return mFrame.iCancelButton.component;
		}
		public function get iScrollBar():ui_scroll_bar
		{
			return mFrame.iScrollBar.component;
		}
		public function get iStaffTable():uiGroup
		{
			return mFrame.iStaffTable.component;
		}
	}

}